<!-- 富文本页面 -->
<template>
    <div>
      <!-- <div>
        <button @click="printEditorHtml">print html</button>
        <button @click="getEditorText">print text</button>
      </div> -->
      <div style="border: 1px solid #ccc; margin-top: 10px">
        <!-- 工具栏 -->
        <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editor"
          :defaultConfig="toolbarConfig"
        />
        <!-- 编辑器 -->
        <Editor
          style="height: 50vh; overflow-y: hidden"
          :defaultConfig="editorConfig"
          v-model="html"
          @onChange="onChange"
          @onCreated="onCreated"
        />
      </div>
      <!-- <div style="margin-top: 10px">
        <textarea
          v-model="html"
          readonly
          style="width: 100%; height: 200px; outline: none"
        ></textarea>
      </div> -->
    </div>
    <!-- <pre type="JavaScript"><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">demo</span><span class="hljs-params">()</span>{</span>}</code></pre> -->
  </template>
  
  <script>
  import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
  import '@wangeditor/editor/dist/css/style.css';

  export default {
    name: "MyEditor",
    components: { Editor, Toolbar },
    props: ['getTextareaContent','sendTextarea','sendNum'],
    data() {
      return {
        textareaContent:'',//html内容
        editor: null,
        html: "<p>&nbsp;</p>",
        // toolbarConfig: {
        //   toolbarKeys: [ /* 显示哪些菜单，如何排序、分组 */ ],
        //   excludeKeys: [ /* 隐藏哪些菜单 */ ],
        // },
        editorConfig: {
          // placeholder: "请输入内容...",
          // autoFocus: false,
          // 所有的菜单配置，都要在 MENU_CONF 属性下
          MENU_CONF: {},
        },
      };
    },
    methods: {
      send(){//把html内容传给父组件
        this.getTextareaContent(this.textareaContent,this.sendNum)
      },
      onCreated(editor) {
        this.editor = Object.seal(editor); // 【注意】一定要用 Object.seal() 否则会报错
      },
      onChange(editor) {
        this.textareaContent = editor.getHtml();
        this.send();
      },
      // getEditorText() {
      //   const editor = this.editor;
      //   if (editor == null) return;
      //   console.log(editor.getText()); // 执行 editor API
      // },
      // printEditorHtml() {
      //   const editor = this.editor;
      //   if (editor == null) return;
      //   console.log(editor.getHtml()); // 执行 editor API
      // },
    },
    mounted() {
      setTimeout(()=>{
        this.html = this.sendTextarea
      },200)
    },
    beforeUnmount() {
      const editor = this.editor;
      if (editor == null){
        editor.destroy(); // 组件销毁时，及时销毁 editor ，重要！！！
      }
    },
  };
  </script>
  
<!-- <style src="@wangeditor/editor/dist/css/style.css">

</style>
  -->